//
//  SBSingleID.m
//  InternationalSDK_New
//
//  Created by huang cole on 14-7-10.
//  Copyright (c) 2014年 sb. All rights reserved.
//

#import "SBSingleID.h"
#import "OpenUDID.h"


#define KEFAppKey @"com.sb.app_key"
#define KEFSingleIdKey @"com.sb.singleId_key"

@implementation SBSingleID

/**
 *  唯一標識
 *
 *  @return 唯一標識串
 */
+ (NSString *)singleID{
    NSMutableDictionary *usernamepasswordKVPairs = (NSMutableDictionary *)[self load:[self key_AppKey]];
    if ([[NSUserDefaults standardUserDefaults] objectForKey:KEFSingleIdKey]) {
        return [[NSUserDefaults standardUserDefaults] objectForKey:KEFSingleIdKey];
    }else if ([usernamepasswordKVPairs objectForKey:[self key_IdKey]]){
        [SBSingleID setSingleID:[usernamepasswordKVPairs objectForKey:[self key_IdKey]]];
        return [usernamepasswordKVPairs objectForKey:[self key_IdKey]];
    }else{
        [SBSingleID setSingleID:[OpenUDID value]];
        return [OpenUDID value];
    }
}

+ (void)setSingleID:(NSString *)singleID{
    NSMutableDictionary *usernamepasswordPairs = [NSMutableDictionary dictionary];
    [usernamepasswordPairs setObject:singleID forKey:[self key_IdKey]];
    [self save:[self key_AppKey] data:usernamepasswordPairs];
    [[NSUserDefaults standardUserDefaults]setObject:singleID forKey:KEFSingleIdKey];
    [[NSUserDefaults standardUserDefaults]synchronize];
}

+ (NSString *)key_IdKey{
    NSString *singleIdKey = [NSString stringWithFormat:KEFSingleIdKey];
    return singleIdKey;
}


+ (NSString *)key_AppKey{
    NSString *appkey = [NSString stringWithFormat:KEFAppKey];
    return appkey;
}


+ (NSMutableDictionary *)getKeychainQuery:(NSString *)service{
    return [NSMutableDictionary dictionaryWithObjectsAndKeys:
            (id)kSecClassGenericPassword,(id)kSecClass,
            service, (id)kSecAttrService,
            service, (id)kSecAttrAccount,
            (id)kSecAttrAccessibleAfterFirstUnlock,(id)kSecAttrAccessible,
            nil];
}


+ (void)save:(NSString *)service data:(id)data{
    //Get search dictionary
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
    //Delete old item before add new item
    SecItemDelete((CFDictionaryRef)keychainQuery);
    //Add new object to search dictionary(Attention:the data format)
    [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(id)kSecValueData];
    //Add item to keychain with the search dictionary
    SecItemAdd((CFDictionaryRef)keychainQuery, NULL);
}


+ (id)load:(NSString *)service{
    id ret = nil;
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
    //Configure the search setting
    //Since in our simple case we are expecting only a single attribute to be returned (the password) we can set the attribute kSecReturnData to kCFBooleanTrue
    [keychainQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
    [keychainQuery setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
    CFDataRef keyData = NULL;
    if (SecItemCopyMatching((CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {
        @try {
            ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData *)keyData];
        } @catch (NSException *e) {
            NSLog(@"Unarchive of %@ failed: %@", service, e);
        } @finally {
        }
    }
    if (keyData)
        CFRelease(keyData);
    return ret;
}


+ (void)delete:(NSString *)service{
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
    SecItemDelete((CFDictionaryRef)keychainQuery);
}

@end
